package tech.lq0.dreamaticvoyage.compat.jei;

import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.gui.builder.IRecipeLayoutBuilder;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.drawable.IDrawableAnimated;
import mezz.jei.api.gui.ingredient.IRecipeSlotsView;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.recipe.IFocusGroup;
import mezz.jei.api.recipe.RecipeIngredientRole;
import mezz.jei.api.recipe.RecipeType;
import mezz.jei.api.recipe.category.IRecipeCategory;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import tech.lq0.dreamaticvoyage.Utils;
import tech.lq0.dreamaticvoyage.init.ItemRegistry;
import tech.lq0.dreamaticvoyage.recipe.FukamizuReactingRecipe;

import javax.annotation.ParametersAreNonnullByDefault;
import java.text.DecimalFormat;

public class FukamizuReactingCategory implements IRecipeCategory<FukamizuReactingRecipe> {

    public static final ResourceLocation TEXTURE = Utils.loc("textures/gui/fukamizu_reaction_chamber.png");
    public static final RecipeType<FukamizuReactingRecipe> TYPE = new RecipeType<>(Utils.loc("fukamizu_reaction_chamber"),
            FukamizuReactingRecipe.class);

    private final IDrawable background;
    private final IDrawable icon;
    private final IDrawableAnimated progress;

    public FukamizuReactingCategory(IGuiHelper helper) {
        this.background = helper.drawableBuilder(TEXTURE, 34, 34, 125, 66)
                .setTextureSize(256, 256)
                .build();

        this.icon = helper.createDrawableIngredient(VanillaTypes.ITEM_STACK, new ItemStack(ItemRegistry.FUKAMIZU_REACTION_CHAMBER.get()));
        this.progress = helper.drawableBuilder(TEXTURE, 182, 0, 22, 16)
                .setTextureSize(256, 256)
                .buildAnimated(200, IDrawableAnimated.StartDirection.LEFT, false);
    }

    @Override
    @ParametersAreNonnullByDefault
    public void draw(FukamizuReactingRecipe recipe, IRecipeSlotsView recipeSlotsView, GuiGraphics guiGraphics, double mouseX, double mouseY) {
        this.progress.draw(guiGraphics, 69, 25);

        var component = Component.translatable("gui.jei.category.smelting.time.seconds", new DecimalFormat("##.#").format(recipe.tick / 20.0f));
        guiGraphics.drawString(Minecraft.getInstance().font,
                component, 120 - Minecraft.getInstance().font.width(component), 54, 5592405, false);
    }

    @Override
    public @NotNull RecipeType<FukamizuReactingRecipe> getRecipeType() {
        return TYPE;
    }

    @Override
    public @NotNull Component getTitle() {
        return Component.translatable("gui.jei.category.dreamaticvoyage.fukamizu_reacting");
    }

    @Override
    public @NotNull IDrawable getBackground() {
        return background;
    }

    @Override
    public @NotNull IDrawable getIcon() {
        return icon;
    }

    @Override
    @ParametersAreNonnullByDefault
    public void setRecipe(IRecipeLayoutBuilder builder, FukamizuReactingRecipe recipe, IFocusGroup group) {
        builder.addSlot(RecipeIngredientRole.INPUT, 25, 25).addIngredients(recipe.input);
        if (!recipe.up.isEmpty()) {
            builder.addSlot(RecipeIngredientRole.INPUT, 25, 3).addIngredients(recipe.up);
        }
        if (!recipe.left.isEmpty()) {
            builder.addSlot(RecipeIngredientRole.INPUT, 3, 25).addIngredients(recipe.left);
        }
        if (!recipe.down.isEmpty()) {
            builder.addSlot(RecipeIngredientRole.INPUT, 25, 47).addIngredients(recipe.down);
        }
        if (!recipe.right.isEmpty()) {
            builder.addSlot(RecipeIngredientRole.INPUT, 47, 25).addIngredients(recipe.right);
        }
        builder.addSlot(RecipeIngredientRole.OUTPUT, 102, 25).addItemStack(recipe.getResultItem(null));
    }

}
